from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import os, sys
from google.appengine.ext.webapp import template

#local imports
from XMLParser import XMLParser

class MainPage(webapp.RequestHandler):
    
    def get(self):
        
        xmlparser = XMLParser()
        
        #get this from the last.fm api
        artistsearchdoc = """<results for="cher" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query role="request" searchTerms="cher" startPage="1"/>
  <opensearch:totalResults>386</opensearch:totalResults>
  <opensearch:startIndex>0</opensearch:startIndex>
  <opensearch:itemsPerPage>20</opensearch:itemsPerPage>
  <artistmatches>
    <artist>
      <name>Cher</name>
      <mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
      <url>www.last.fm/music/Cher</url>
      <image_small>http://userserve-ak.last.fm/serve/50/342437.jpg</image_small>
      <image>http://userserve-ak.last.fm/serve/160/342437.jpg</image>
      <streamable>1</streamable>
    </artist>
    <artist>
      <name>Cher2</name>
      <mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
      <url>www.last.fm/music/Cher2</url>
      <image_small>http://userserve-ak.last.fm/serve/50/342437.jpg222</image_small>
      <image>http://userserve-ak.last.fm/serve/160/342437.jpg222</image>
      <streamable>1</streamable>
    </artist>
  </artistmatches>
</results>"""

        songsearchdoc = """
        <results for="Believe" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query role="request" searchTerms="Believe" startPage="1"/>
  <opensearch:totalResults>25329</opensearch:totalResults>
  <opensearch:startIndex>0</opensearch:startIndex>
  <opensearch:itemsPerPage>20</opensearch:itemsPerPage>
  <trackmatches>
    <track>
      <name>Believe</name>
      <artist>Disturbed</artist>
      <url>http://www.last.fm/music/Disturbed/_/Believe</url>
      <streamable fulltrack="0">1</streamable>
      <listeners>66068</listeners>
      <image size="small">...</image>
    </track>
    ...
  </trackmatches>
</results>"""

        artistinfodoc = """
    <artist>
  <name>Cher</name>
  <mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
  <url>http://www.last.fm/music/Cher</url>
  <image size="small">http://userserve-ak.last.fm/serve/50/285717.jpg</image>
  <image size="medium">http://userserve-ak.last.fm/serve/85/285717.jpg</image>
  <image size="large">http://userserve-ak.last.fm/serve/160/285717.jpg</image>
  <streamable>1</streamable>
  <stats>
    <listeners>196440</listeners>
    <plays>1599101</plays>
  </stats>
  <similar>
    <artist>
      <name>Madonna</name>
      <url>http://www.last.fm/music/Madonna</url>
      <image size="small">http://userserve-ak.last.fm/serve/50/5112299.jpg</image>
      <image size="medium">http://userserve-ak.last.fm/serve/85/5112299.jpg></image>
      <image size="large">http://userserve-ak.last.fm/serve/160/5112299.jpg</image>
    </artist>
    ...
  </similar>
  <tags>
    <tag>
      <name>pop</name>
      <url>http://www.last.fm/tag/pop</url>
    </tag>
    ...
  </tags>
  <bio>
    <published>Thu, 13 Mar 2008 03:59:18 +0000</published>
    <summary>...</summary>
    <content>...</content>
  </bio>
</artist>"""
        
        template_values = {
            'textvalue': 'mershed perderder',
            'artist_search': xmlparser.parse_artist_search(artistsearchdoc),
            'song_search':xmlparser.parse_song_search(songsearchdoc),
            'artist_info':xmlparser.parse_artist_info(artistinfodoc)
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
        self.response.out.write(template.render(path, template_values))


application = webapp.WSGIApplication([('/', MainPage)], debug=True)



def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
